package com.apollo.android.um.utils;

import android.content.Context;

import com.apollo.android.um.imp.UMessageHandlerImp;
import com.apollo.android.um.imp.UNotificationClickHandlerImp;
import com.apollo.android.um.imp.URegisterImp;
import com.apollo.android.utils.Helper;
import com.apollo.android.utils.ThreadPools;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;

import java.util.HashMap;
import java.util.Map;

public class UmUtils {

    /**
     * @param context
     * @param appKey
     * @param secret      推送的secret
     * @param channel
     * @param useMobClick true 是否使用统计功能
     */
    public static void init(Context context, String appKey, String secret,
                            String channel, boolean useMobClick, boolean debug) {
        UMConfigure.setLogEnabled(debug);
        /**
         * 初始化common库
         * 参数1:上下文，不能为空
         * 参数2:【友盟+】 AppKey
         * 参数3:【友盟+】 Channel
         * 参数4:设备类型，UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子，默认为手机
         * 参数5:Push推送业务的secret
         */
        UMConfigure.init(context, appKey, channel, UMConfigure.DEVICE_TYPE_PHONE, secret);
        if (Helper.isNotEmpty(secret)) {
            initPush(context);
        }
        if (useMobClick) {
            UMConfigure.setProcessEvent(true);
            MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
        }
    }

    /**
     * 初始化推送
     *
     * @param context
     */
    private static void initPush(Context context) {
        final PushAgent pushAgent = PushAgent.getInstance(context);
        pushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);
        pushAgent.setMessageHandler(new UMessageHandlerImp());
        pushAgent.setNotificationClickHandler(new UNotificationClickHandlerImp());
        pushAgent.register(new URegisterImp());
    }

    /**
     * 开始统计
     *
     * @param name
     */
    public static void onPageStart(String name) {
        MobclickAgent.onPageStart(name);
    }

    /**
     * 结束统计
     *
     * @param name
     */
    public static void onPageStop(String name) {
        MobclickAgent.onPageEnd(name);
    }

    /**
     * 事件上报
     *
     * @param context
     * @param eventID
     * @param params
     */
    public static void onEventReport(Context context, String eventID, HashMap<String, String> params) {
        MobclickAgent.onEvent(context, eventID, params);
    }
}
